package com.dh.edu.gateway.filter;

import com.dh.edu.gateway.rule.RibbonParaContext;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;


/**
 * 灰度过滤器
 *
 * @Date Created in 2022/8/16 14:10
 */
@Component
public class GrayFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String userId = exchange.getRequest().getHeaders().get("userId").get(0);
        Map<String, String> map = new HashMap<>();
        map.put("userId", userId);
        RibbonParaContext.set(map);
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
